<?php

namespace App\Models\Post;

use App\Libraries\Easemob\Easemob;
use App\Libraries\Tools\URL;
use App\Models\BaseModel;
use App\Models\User\Follow;
use App\Models\User\User;
use Illuminate\Support\Facades\Auth;

class PostComment extends BaseModel
{

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [
        'updated_at'
    ];
    protected $dateFormat = 'U';

    /**
     * 关联用户
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function user(){
        return $this->belongsTo('App\Models\User\User', 'user_id', 'user_id');
    }

    /**
     * 关联回复用户
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function reply_user(){
        return $this->belongsTo('App\Models\User\User', 'reply_user_id', 'user_id')->where('user_id', '<>', 0);
    }

    /**
     * 关联积分
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function points(){
        return $this->hasOne('App\Models\User\UserPoint', 'user_id', 'user_id');
    }

    /**
     * 获取主评论
     * @param $page
     * @param $size
     * @param $post_id
     * @return array
     */
    public function getComments($page, $size, $post_id){
        $data = [];
        $data['list'] = $this->select('post_comments.*')
            ->rightJoin('users',function($join) {
                $join->on('post_comments.user_id', '=', 'users.user_id')->where('users.status', 1);
            })
            ->withOnly('user', ['user_id', 'cover', 'nickname', 'vip_level'])
            ->with('points')
            ->where('post_comments.status', 1)
            ->where('post_comments.comment_id', 0)
            ->where('post_comments.reply_user_id', 0)
            ->where('post_comments.post_id', $post_id)
            ->forPage($page, $size+1)
            ->orderBy('post_comments.commented_at', 'desc')
            ->get()
            ->toArray();
        $data['next_page'] = $page + 1;
        $data['page_size'] = $size;
        return $this->after($data);
    }

    /**
     * 获取某条评论
     * @param $comment_id
     * @return mixed
     */
    public function getComment($comment_id){
        $data = $this->select('post_comments.*')
            ->rightJoin('users',function($join) {
                $join->on('post_comments.user_id', '=', 'users.user_id')->where('users.status', 1);
            })
            ->withOnly('user', ['user_id', 'cover', 'nickname', 'vip_level'])
            ->where('post_comments.status', 1)
            ->where('post_comments.comment_id', 0)
            ->where('post_comments.reply_user_id', 0)
            ->where('post_comments.id', $comment_id)
            ->first()
            ->toArray();
        return $this->after($data);
    }

    /**
     * 获取子评论
     * @param $page
     * @param $size
     * @param $comment_id
     * @return mixed
     */
    public function getSubComments($page, $size, $comment_id){
        $data = [];
        $data['list'] = $this->select('post_comments.*')
            ->rightJoin('users',function($join) {
                $join->on('post_comments.user_id', '=', 'users.user_id')->where('users.status', 1);
            })
            ->withOnly('user', ['user_id', 'cover', 'nickname', 'vip_level'])
            ->withOnly('reply_user', ['user_id', 'cover', 'nickname', 'vip_level'])
            ->where('post_comments.status', 1)
            ->where('post_comments.comment_id', $comment_id)
            ->forPage($page, $size+1)
            ->orderBy('post_comments.created_at', 'asc')
            ->get()
            ->toArray();
        $data['next_page'] = $page + 1;
        $data['page_size'] = $size;
        return $this->after($data);
    }

    /**
     * 子评论条数
     * @param $comment_id
     * @return mixed
     */
    public function getSubCommentCount($comment_id){
        return $this->rightJoin('users',function($join) {
                $join->on('post_comments.user_id', '=', 'users.user_id')->where('users.status', 1);
            })
            ->where('post_comments.status', 1)
            ->where('post_comments.comment_id', $comment_id)
            ->count();
    }
}
